package xml;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class DeptparserDom4j {
	public static void main(String[] args) throws DocumentException {
		Dept dept = new Dept();
		Emp emp;
		SAXReader reader = new SAXReader();
		Document doc = reader.read(new File(
				"/home/tarena/0hean/hean/src/xml/dept.xml"));
		Element root = doc.getRootElement();
		//可用element方法
		Node node = doc.selectSingleNode("/dept/deptname");
		dept.setDeptname(node.getText());
		node = doc.selectSingleNode("/dept/@id");
		dept.setId(Integer.parseInt(node.getText()));
		node = doc.selectSingleNode("/dept/@establishtime");
		dept.setEstablishtime(node.getText());
		node = doc.selectSingleNode("/dept/mgr");
		dept.setMgr(node.getText());
		System.out.println(dept);

		List<Element> nodes = doc.selectNodes("/dept/emps/emp");
		for (Element e : nodes) {
			emp = new Emp();
			dept.getEmpList().add(emp);
			emp.setId(Integer.parseInt(e.element("id")
					.getText()));
			emp.setName(e.element("name").getText());
			emp.setSalary(Double.parseDouble(e.element("salary").getText()));
			System.out.println("emp:"+emp);
		}
		
	}
}
